package com.example.controller.frontend;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.common.ApiResult;
import com.example.controller.UploadController;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.vo.ForgotPwdVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RestController
@Api(tags = "用户管理")
@RequestMapping("/api/upload")
public class FUserController {
    @Resource
    private UserService userService;

    @Autowired
    private UploadController uploadController;

    @ApiOperation("上传用户头像")
    @PostMapping("/updateimg")
    public ApiResult updateimg(@RequestParam(required = true,value = "id") Integer id,
                               @RequestPart(required = false,value = "upFile") MultipartFile upFile, HttpServletRequest request){
        ApiResult apiResult = uploadController.uploadAvatar(upFile,request);
        if (apiResult.isSuccess() == true) {
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("id",id);
            updateWrapper.set("avatar_url",apiResult.getData());
            boolean update = userService.update(updateWrapper);
            if (update==true){
                return ApiResult.ok(apiResult.getData(),"头像上传成功");
            }else {
                return ApiResult.fail("头像上传失败");
            }
        }else {
            return apiResult;
        }
    }

    @ApiOperation("修改用户昵称")
    @PostMapping("/updateNickName")
    public ApiResult updateNickName(@RequestParam Integer id, @RequestParam String NickName){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("nick_name",NickName);
        boolean b = userService.update(updateWrapper);
        return ApiResult.ok(b);
    }

    @ApiOperation("用户登录")
    @PostMapping("/signin")
    public ApiResult signin(@RequestParam(required = true) String phoneNumber,
                            @RequestParam(required = true) String password){
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("phone_number",phoneNumber);
        queryWrapper.eq("password",password);
        User user = userService.getOne(queryWrapper);
        if (user==null){
            return ApiResult.fail();
        }
        return ApiResult.ok(user);
    }

    @Resource
    private SmsController smsController;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ApiResult register(@RequestParam(required = true) String phoneNumber,
                              @RequestParam(required = true) String password,
                              @RequestParam(required = true) String nickName,
                              @RequestParam(required = true) String inputCode){

        ApiResult apiResult = smsController.checkPhoneLogin(phoneNumber, inputCode);
        if (apiResult.isSuccess()==true){
            boolean register = userService.register(phoneNumber, password, nickName);
            if (register == false) {
                return ApiResult.fail("注册失败,手机号以存在");
            }else {
                return apiResult;
            }
        }
        return apiResult;
    }

    @ApiOperation("忘记密码")
    @PostMapping("/ForgotPassword")
    public ApiResult ForgotPassword(@RequestBody ForgotPwdVO forgotPwdVO) {
        ApiResult apiResult = userService.forgotPassword(forgotPwdVO);
        return apiResult;
    }
}
